#include "MainScene.h"
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
USING_NS_CC;

HelloWorld::HelloWorld()
{

}

HelloWorld::~HelloWorld()
{

}

void HelloWorld::SetAllItemVisable(bool Visable)
{
	if (getChildren().empty())
		return;

	for (int i = 0; i < getChildren().size(); i++)
	{
		getChildren().at(i)->setVisible(Visable);
	}
}

bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
	m_TouchedButton = -1;
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("BackGround_HelloScene.mp3", true);
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    

	Sprite* AlphaLogo = Sprite::create("Alpha_Logo.png");
	AlphaLogo->SetPosition(visibleSize.width / 2, visibleSize.height * 0.75);
	AlphaLogo->setOpacity(0);
	AlphaLogo->runAction(Sequence::create(DelayTime::create(2.0f), FadeIn::create(2.0f), NULL));
	addChild(AlphaLogo);

	Sprite* PlayFunLogo = Sprite::create("PlayFun_Logo.png");
	PlayFunLogo->setScale(0.3f);
	PlayFunLogo->setPosition(AlphaLogo->getPositionX(), AlphaLogo->getPositionY() - PlayFunLogo->getContentSize().height);
	PlayFunLogo->setOpacity(0);
	PlayFunLogo->runAction(Sequence::create(DelayTime::create(4.0f), FadeIn::create(2.0f), NULL));
	addChild(PlayFunLogo);

	particleSystem = ParticleGalaxy::create();
	particleSystem->setTexture(CCTextureCache::sharedTextureCache()->addImage("star.png"));
	particleSystem->setPosition(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2);
	addChild(particleSystem);

	for (int i = 0; i != 4; i++)
	{
		Sprite* Temp = Sprite::create(GetUrlByTag((HelloWorldMenuTag)i).c_str());
		Temp->SetPosition(visibleSize.width / 2, visibleSize.height * 0.43 - (i * Temp->getBoundingBox().size.height));
		Temp->setScale(0.5f);
		Temp->setTag(i);
		Temp->setOpacity(0);
		Sequence* TempSq = Sequence::create(DelayTime::create(6.0 + 1.0 * i), FadeIn::create(0.5f), i == 3 ? CCCallFunc::create(CC_CALLBACK_0(HelloWorld::LoadFinish, this)) : NULL, NULL);
		Temp->runAction(TempSq);
		Buttons.push_back(Temp);
		addChild(Temp);
	}
    return true;
}


void HelloWorld::LoadFinish()
{
	auto listener = EventListenerTouchOneByOne::create();
	listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
	listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}


bool HelloWorld::onTouchBegan(Touch *touch, Event *unused_event)
{
	if (sSettingLayer->isVisible())
		return false;
	for (int i = 0; i < Buttons.size(); i++)
	{
		Sprite* Temp = Buttons.at(i);
		Vec2 touchLocation = touch->getLocation();
		touchLocation = Temp->getParent()->convertToNodeSpace(touchLocation);
		Rect bBox = Temp->getBoundingBox();
		bool Touched = bBox.containsPoint(touchLocation);
		if (Touched)
		{
			Temp->setTexture(GetUrlByTag((HelloWorldMenuTag)Temp->getTag(), true).c_str());
			m_TouchedButton = Temp->getTag();
			return true;
		}
	}
	return false;
}

void HelloWorld::onTouchMoved(Touch *touch, Event *unused_event){}

void HelloWorld::onTouchEnded(Touch *touch, Event *unused_event)
{
	if (m_TouchedButton < 0)
		return;
	if (Sprite* Temp = (Sprite*)getChildByTag(m_TouchedButton))
	{
		Vec2 touchLocation = touch->getLocation();
		touchLocation = Temp->getParent()->convertToNodeSpace(touchLocation);
		Rect bBox = Temp->getBoundingBox();
		if (bBox.containsPoint(touchLocation))
		{
			switch (Temp->getTag())
			{
				case TAG_SINGLE:
				{
					//sMainScene->SwapLayerByTag(getTag(), TYPING_LAYER_TAG);
					break;
				}
				case TAG_MULTI:
				{
					sMainScene->SwapLayerByTag(getTag(), TYPING_LAYER_TAG);
					return;
				}
				case TAG_SETTINGS:
				{
					SetAllItemVisable(false);
					sSettingLayer->setVisible(true);
					break;
				}
				case TAG_EXIT:
				{
					Director::getInstance()->end();
					#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
						exit(0);
					#endif
					break;
				}
			}
		}
		else
		{

		}
		Temp->setTexture(GetUrlByTag((HelloWorldMenuTag)Temp->getTag()).c_str());
		m_TouchedButton = -1;
	}
	else
	{

	}
}

std::string HelloWorld::GetUrlByTag(HelloWorldMenuTag _HelloWorldMenuTag, bool IsTouched)
{
	std::string ret = "";
	switch (_HelloWorldMenuTag)
	{
	case TAG_SINGLE:
		IsTouched == false ?
			ret = "Menu_Button_0.png" :
			ret = "Menu_Button_0_1.png";
		break;
	case TAG_MULTI:
		IsTouched == false ?
			ret = "Menu_Button_1.png" :
			ret = "Menu_Button_1_1.png";
		break;
	case TAG_SETTINGS:
		IsTouched == false ?
			ret = "Menu_Button_2.png" :
			ret = "Menu_Button_2_1.png";
		break;
	case TAG_EXIT:
		IsTouched == false ?
			ret = "Menu_Button_3.png" :
			ret = "Menu_Button_3_1.png";
		break;
	}

	return ret;
}


SettingsLayer::SettingsLayer()
{
	CanPlayMusic = true;
	m_TouchedButton = -1;
}

SettingsLayer::~SettingsLayer()
{
}


bool SettingsLayer::init()
{
	if (!Layer::init())
	{
		return false;
	}
	Size visibleSize = Director::getInstance()->getVisibleSize();
	Vec2 origin = Director::getInstance()->getVisibleOrigin();

	auto listener = EventListenerTouchOneByOne::create();
	listener->onTouchBegan = CC_CALLBACK_2(SettingsLayer::onTouchBegan, this);
	listener->onTouchEnded = CC_CALLBACK_2(SettingsLayer::onTouchEnded, this);
	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);


	BackGround = Sprite::create("TalkClass_BackGround.png");
	BackGround->SetPosition(visibleSize.width / 2, visibleSize.height / 2);
	addChild(BackGround);

	Sprite* Title = Sprite::create("Setting_Title.png");
	Title->setPosition(BackGround->getContentSize().width / 2, BackGround->getContentSize().height * 0.96 - Title->getBoundingBox().size.height / 2);
	BackGround->addChild(Title);
	float SingleY = BackGround->getBoundingBox().size.height / 6;
	float FirstY = BackGround->getBoundingBox().size.height / 8;

	for (int i = 0; i < 5; i++)
	{
		Sprite* TempSettingFrame = new Sprite();
		TempSettingFrame->initWithFile("Setting_Menu_Button.png");
		TempSettingFrame->autorelease();
		TempSettingFrame->setPosition(BackGround->getContentSize().width / 2, BackGround->getContentSize().height - BackGround->getBoundingBox().size.height + FirstY + (SingleY * i));
		TempSettingFrame->setTag(i);
		BackGround->addChild(TempSettingFrame);
		Sprite* TempSettingText = new Sprite();
		char msg[255];
		snprintf(msg, 255, "Setting_Button_%d.png", i);
		TempSettingText->initWithFile(msg);
		TempSettingText->autorelease();
		TempSettingText->setPosition(TempSettingFrame->getContentSize().width * 0.1 + TempSettingText->getBoundingBox().size.width / 2, TempSettingFrame->getContentSize().height / 2);
		TempSettingFrame->addChild(TempSettingText);
		SettingButton.push_back(TempSettingFrame);

		if (i == 4)
		{
			Sprite* TempOk = Sprite::create("Ok_Image.png");
			TempOk->setPosition(TempSettingText->getPositionX() + TempSettingText->getBoundingBox().size.width, TempSettingFrame->getPositionY());
			BackGround->addChild(TempOk);
		}
	}
	return true;
}

bool SettingsLayer::onTouchBegan(Touch *touch, Event *unused_event)
{
	if (!sSettingLayer->isVisible())
		return false;

	for (int i = 0; i != SettingButton.size(); i++)
	{
		Sprite* Temp = SettingButton.at(i);
		Vec2 touchLocation = touch->getLocation();
		touchLocation = Temp->getParent()->convertToNodeSpace(touchLocation);
		Rect bBox = Temp->getBoundingBox();
		bool Touched = bBox.containsPoint(touchLocation);
		if (Touched)
		{
			m_TouchedButton = Temp->getTag();
			switch (Temp->getTag())
			{
				case TAG_MUSIC:
					break;
				case TAG_SAVEGAME:
					break;
				case TAG_LOADGAME:
					break;
				case TAG_RETURN_MAIN:
					break;
				case TAG_EXIT_GAME:
					break;
			}
			return true;
		}
	}
	return false;
}

void SettingsLayer::onTouchEnded(Touch *touch, Event *unused_event)
{
	if (m_TouchedButton < 0)
		return;

	Sprite* Temp = (Sprite*)BackGround->getChildByTag(m_TouchedButton);
	Vec2 touchLocation = touch->getLocation();
	touchLocation = Temp->getParent()->convertToNodeSpace(touchLocation);
	Rect bBox = Temp->getBoundingBox();
	if (bBox.containsPoint(touchLocation))
	{
		switch (m_TouchedButton)
		{
		case TAG_MUSIC:
			if (CanPlayMusic)
			{
				CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
				CanPlayMusic = false;
			}
			else
			{
				CocosDenshion::SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
				CanPlayMusic = true;
			}
			break;
		case TAG_SAVEGAME:
			break;
		case TAG_LOADGAME:
			break;
		case TAG_RETURN_MAIN:
			sMainLayer->SetAllItemVisable(true);
			setVisible(false);
			break;
		case TAG_EXIT_GAME:
			Director::getInstance()->end();
			#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
			exit(0);
			#endif
			break;
		}
	}
	m_TouchedButton = -1;
}